﻿using Common;
using Common.Helper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Quartz.Manage.ApiControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SystemController : ControllerBase
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public ApiResult Login([FromBody] AdminModel data)
        {
            string AdminUserName = ConfigHelper.Configuration["AdminUserName"];
            string AdminPwd = ConfigHelper.Configuration["AdminPwd"];
            if (AdminUserName.IsNullOrWhiteSpace())
            {
                AdminUserName = "admin";
            }
            if (AdminPwd.IsNullOrWhiteSpace())
            {
                AdminPwd = "123456";
            }
            var res = new ApiResult { Message = "登录失败", Success = false };
            if (data.UserName == AdminUserName && data.Pwd== AdminPwd)
            {
                var CookiesBody = HttpContext.Request.Cookies["Quartz.Admin"];
                //如果存在则删除原Cookies
                if (!string.IsNullOrWhiteSpace(CookiesBody))
                {
                    HttpContext.Response.Cookies.Delete("Quartz.Admin");
                }
                //增加新Cookies
                HttpContext.Response.Cookies.Append("Quartz.Admin", AESEncryptHelper.Encode(data.ToJson()));
                res.Success = true;
                res.Message = "登录成功";
            }
            return res;
        }
    }
}
